package main
import(
	"fmt"
	"net/http"
	"io/ioutil"
	"text/template"
)

func main(){
	http.HandleFunc("/", index)
	http.HandleFunc("/uploadafile", uploadafile)
	fmt.Println("Listen and serve ...")
	http.ListenAndServe(":8000", nil)
}

func index(writer http.ResponseWriter, request *http.Request){
	f, err := template.ParseFiles("./file.html")
	t := template.Must(f, err)
	t.Execute(writer, nil)
}

func uploadafile(writer http.ResponseWriter, request *http.Request){
	/*注意：html文件里面*/
	/*form 必须有 enctype="multipart/form-data"*/
	/*form 必须是 method="post"*/
	file, _, err := request.FormFile("uploaded")
	if err != nil{
		fmt.Println("ReadAll 1 == ERR : ", err)
		return
	}
	data, err := ioutil.ReadAll(file)
	if err != nil{
		fmt.Println("ReadAll 2 == ERR : ", err)
		return
	}
	fmt.Fprintln(writer, string(data))
	fmt.Println("ReadAll OK !")
}
